package pl.bmajsak.dzone.pages;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

/**
 * This class follows 
 * <a href="http://code.google.com/p/selenium/wiki/PageObjects" title="Page Object pattern">Page Object pattern</a>
 * and provides possibility to retrieve all displayed titles.
 * 
 * @author Bartosz Majsak
 *
 */
public class DzoneSearchResultPage {

	private static final String SEARCH_RESULT_TITLES_XPATH = "//div[@class='details']/h3/a";

	private final WebDriver driver;

	public DzoneSearchResultPage(WebDriver driver) {
		super();
		this.driver = driver;
	}

	public List<String> getSearchResultTitles() {
		List<WebElement> elements = driver.findElements(By.xpath(SEARCH_RESULT_TITLES_XPATH));
		List<String> results = Lists.transform(elements, new ExtractText());
		return results;
	}

	/**
	 * 
	 * Function object for extracting text nested in 'title' elements.
	 * Used in conjuction with Google Collection transformation facility.
	 * 
	 *
	 */
	private final class ExtractText implements Function<WebElement, String> {
		@Override
		public String apply(WebElement from) {
			return from.getText();
		}
	}
}
